package com.nf.health.app.inquiry.docdetail;

import android.os.Bundle;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.mydoctor.adapter.GzDoctorAdapter;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.models.DoctorVo;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的医生列表（单独）
 * 详情请参考：com.nf.health.app.fragment。DoctorActivity
 */
public class MyDoctorListActivity extends BaseActivity {
    @Bind(R.id.consul_listview)
    DragListView consulListview;
    @Bind(R.id.titlebar_title_tv)
    TextView titlebar_title_tv;
    private int page = 1;
    private int pageSize = 20;
    private final String type = "attention";
    private List<DoctorVo> mlist = new ArrayList<DoctorVo>();
    private GzDoctorAdapter adapterDoctor2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_mydoctorlistactivity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        titlebar_title_tv.setText("我的医生");
        consulListview.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData(page, "refresh2");
            }

            @Override
            public void onLoadMore() {
                page++;
                loadData(page, "more2");
            }
        });
    }

    private void loadData(int page,String flag){
        provider.requestMyDoctorList(type, page + "", pageSize + "", flag);
    }
    public void onResume() {
        super.onResume();
        page = 1;
        loadData(page, "refresh2");
    }
    @SuppressWarnings("unchecked")
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if ("refresh2".equals(actionName)) {
            consulListview.completeRefresh();
            List<DoctorVo> result = (List<DoctorVo>) object;
            if(result.size() < pageSize){
                consulListview.setRefreshableAndLoadMoreable(true,false);
            }else{
                consulListview.setRefreshableAndLoadMoreable(true,true);
            }
            mlist.clear();
            mlist.addAll(result);
        } else if ("more2".equals(actionName)) {
            consulListview.completeLoadMore();
            List<DoctorVo> result = (List<DoctorVo>) object;
            if(result.size() < pageSize){
                consulListview.setRefreshableAndLoadMoreable(true,false);
            }else{
                consulListview.setRefreshableAndLoadMoreable(true,true);
            }
            mlist.addAll(result);
        }
        if(adapterDoctor2 == null){
            adapterDoctor2 = new GzDoctorAdapter(this,mlist);
            consulListview.setAdapter(adapterDoctor2);
        }else{
            adapterDoctor2.setMyGzDoctorNotifyDataChanged(mlist);
        }
        adapterDoctor2.setOnMyClickListener(new OnMyClickListener() {
            @Override
            public void onClick(int position, String flag) {
                provider.requestCancleFoucsDoctor(flag, "deleteAttention");

            }
        });
    }
}
